# coding: utf-8
# @Author: 莫小鱼
import time

import pytest

from AutoWebTest.config import setting
from AutoWebTest.config.setting import is_dd_msg
from AutoWebTest.utils.connectmysql import ConnectMysql
from AutoWebTest.utils.emialReport import send_dd_msg
from AutoWebTest.utils.log_util import logs


# @pytest.fixture(scope='session', autouse=True)
# def data_cleaning():
#     "测试结束后清理测试数据"
#     conn = ConnectMysql()
#     yield
#     logs.info("正在清理测试数据......")
#     sql = "delete from ecs_users where user_name='test01'"
#     conn.delete(sql)


def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """pytest预定义的钩子函数，用于自动收集测试结果"""
    total = terminalreporter._numcollected
    passed = len(terminalreporter.stats.get('passed', []))
    failed = len(terminalreporter.stats.get('failed', []))
    error = len(terminalreporter.stats.get('error', []))
    skipped = len(terminalreporter.stats.get('skipped', []))
    duration = round(time.time() - terminalreporter._sessionstarttime, 2)
    summary = f"""
    自动化测试结果，通知如下，具体执行结果如下：
    测试用例总数：{total}
    测试通过数：{passed}
    测试失败数：{failed}
    测试错误数：{error}
    测试执行跳过数：{skipped}
    测试执行总时长：{duration}s
    """
    if is_dd_msg:
        send_dd_msg(summary)
